package com.fjec.common.shiro;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;

public final class AccountInfo {

    /*--------------------------------------------
    |             C O N S T A N T S             |
    ============================================*/
    public static final String ATTR_PREFIX = AccountInfo.class.getName();

    public static final String SESSION_ATTR_USER_ID = ATTR_PREFIX + ".userId";

    public static final String SESSION_ATTR_USER_OPENID = ATTR_PREFIX + ".openId";

    public static final String SESSION_ATTR_USER_ROLE = ATTR_PREFIX + ".role";

    /**
     * 初始化账户信息
     *
     * @param userId 用户ID
     * @param openId 用户微信openID
     */
    public static void init(Long userId, String openId, String role) {
        Session session = getSession();
        session.setAttribute(SESSION_ATTR_USER_ID, userId);
        session.setAttribute(SESSION_ATTR_USER_OPENID, openId);
        session.setAttribute(SESSION_ATTR_USER_ROLE, role);
    }

    /**
     * 删除账户信息
     */
    public static void clean() {
        Session session = getSession();
        session.removeAttribute(SESSION_ATTR_USER_ID);
        session.removeAttribute(SESSION_ATTR_USER_OPENID);
        session.removeAttribute(SESSION_ATTR_USER_ROLE);
    }

    /**
     * 获取SESSION
     */
    public static Session getSession() {
        return SecurityUtils.getSubject().getSession();
    }

    /**
     * 获取用户ID
     *
     * @return
     */
    public static Long getUserId() {
        return (Long) getSession().getAttribute(SESSION_ATTR_USER_ID);
    }

    /**
     * 获取微信OPENID
     *
     * @return
     */
    public static String getOpenId() {
        return (String) getSession().getAttribute(SESSION_ATTR_USER_OPENID);
    }
    /**
     * 获取角色
     *
     * @return
     */
    public static String getRole() {
        return (String) getSession().getAttribute(SESSION_ATTR_USER_ROLE);
    }
}
